package com.xxx.mch.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.mch.domain.dto.QualityScoreTemplateDto;
import com.xxx.mch.domain.po.QualityScore;
import com.xxx.mch.domain.request.DeltaUpdateRequest;
import com.xxx.mch.domain.request.QualityScoreTemplateRequest;
import com.xxx.mch.domain.request.QueryScoreRequest;
import com.xxx.mch.domain.request.RuleSearchRequest;
import com.xxx.mch.service.QualityScoreService;
import com.xxx.mch.service.TotalScoreService;
import com.xxx.mch.utils.WrapMapper;
import com.xxx.mch.utils.Wrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 质量评分
 *
 * @author FengHaixin
 * @description 一句话描述该文件的用途
 * @date 2020/2/26
 */
@RestController
@RequestMapping ("qualityScores")
public class QualityScoreController
{
    @Autowired
    private QualityScoreService qualityScoreService;

    @Autowired
    private TotalScoreService totalScoreService;


    @PostMapping (consumes = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper saveQualityScore(@RequestBody QualityScore qualityScore)
    {
        qualityScoreService.saveScore(qualityScore);
        return WrapMapper.ok();
    }


    @GetMapping (produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<PageInfo<QualityScore>> getQualityScore(@Validated QueryScoreRequest queryScoreRequest)
    {
        PageInfo<QualityScore> pageInfo = qualityScoreService.queryScoreRecord(queryScoreRequest);
        return WrapMapper.ok(pageInfo);
    }


    /**
     * 获取评选类型筛选类型
     *
     * @param queryScoreRequest
     *
     * @return
     */
    @GetMapping (value = "evaluateType", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<QualityScore>> queryEvaluateFilter(@ModelAttribute @Validated QueryScoreRequest queryScoreRequest)
    {
        return WrapMapper.ok(qualityScoreService.queryEvaluateFilter(queryScoreRequest));
    }


    /**
     * 获取得分类别筛选类型
     *
     * @param queryScoreRequest
     *
     * @return
     */
    @GetMapping (value = "levelType", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<QualityScore>> queryLevelFilter(@ModelAttribute @Validated QueryScoreRequest queryScoreRequest)
    {
        return WrapMapper.ok(qualityScoreService.queryLevelFilter(queryScoreRequest));
    }


    @GetMapping (value = "template", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<QualityScoreTemplateDto>> taskTemplate(@RequestParam (value = "parentId", required = false) Integer parentId,
                                                               @RequestParam (value = "depth", defaultValue = "4") Integer depth)
    {
        return WrapMapper.ok(qualityScoreService.getTaskTemplate(parentId, depth));
    }


    @ApiOperation (value = "质量分细则搜索")
    @GetMapping (value = "rule/search", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<QualityScoreTemplateDto>> ruleSearch(@ModelAttribute @Validated RuleSearchRequest ruleSearchRequest)
    {
        return WrapMapper.ok(qualityScoreService.ruleSearch(ruleSearchRequest));
    }


    @PostMapping (value = "template", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper createOrUpdate(@RequestBody List<QualityScoreTemplateRequest> templateRequest)
    {
        qualityScoreService.createOrUpdateTemplate(templateRequest);
        return WrapMapper.ok();
    }

    @ApiOperation (value = "定量分修正")
    @PostMapping (value = "delta/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<String> deltaUpdate(@RequestBody DeltaUpdateRequest request)
    {
        totalScoreService.deltaUpdate(request);
        return WrapMapper.ok();
    }


    @DeleteMapping (value = "template/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper deleteTemplate(@PathVariable ("id") Integer id)
    {
        qualityScoreService.deleteTemplate(id);
        return WrapMapper.ok();
    }
}
